#include "CCMultiTouchMen.h"
using namespace cocos2d;
CCMultiTouchMenu * CCMultiTouchMenu::create()
{
	CCMultiTouchMenu * mmenu = new CCMultiTouchMenu();
	if(mmenu && mmenu->init())
	{
		mmenu->autorelease();
		return mmenu;
	}
	CC_SAFE_DELETE(mmenu);
	return NULL;
}
bool CCMultiTouchMenu::init()
{
	CCMenu::init();
	CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,kCCMenuHandlerPriority);
	return true;
}
void CCMultiTouchMenu::onEnter()
{

}
void CCMultiTouchMenu::onExit()
{

}
void CCMultiTouchMenu::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CC_UNUSED_PARAM(pEvent);
	if (m_eState != kCCMenuStateWaiting || ! m_bIsVisible || !m_bEnabled)
	{
		return ;
	}

	for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
	{
		if (c->isVisible() == false)
		{
			return ;
		}
	}

	for(CCSetIterator it = pTouches->begin();it!=pTouches->end();it++)
	{
		CCTouch *touchCur = (CCTouch *)*it;
		touchCur->locationInView();
		m_pSelectedItem = this->itemForTouch(touchCur);
		if (m_pSelectedItem)
		{
			m_eState = kCCMenuStateWaiting;
			m_pSelectedItem->selected();
			m_pSelectedItem->activate();
			return;
		}
	}
}
void CCMultiTouchMenu::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	for(CCSetIterator it = pTouches->begin();it!=pTouches->end();it++)
	{
		CCTouch *touchCur = (CCTouch *)*it;
		touchCur->locationInView();
		if (m_pSelectedItem)
		{
			m_eState = kCCMenuStateWaiting;
			m_pSelectedItem->unselected();
			m_pSelectedItem->activate();
			return;
		}
	}
}
void CCMultiTouchMenu::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	for(CCSetIterator it = pTouches->begin();it!=pTouches->end();it++)
	{
		CCTouch *touchCur = (CCTouch *)*it;
		touchCur->locationInView();
		if (m_pSelectedItem)
		{
			m_eState = kCCMenuStateWaiting;
			m_pSelectedItem->unselected();
			m_pSelectedItem->activate();
			return;
		}
	}
}
void CCMultiTouchMenu::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	for(CCSetIterator it = pTouches->begin();it!=pTouches->end();it++)
	{
		CCTouch *touchCur = (CCTouch *)*it;
		touchCur->locationInView();
		CCMenuItem *currentItem = this->itemForTouch(touchCur);
		if (currentItem != m_pSelectedItem) 
		{
			if (m_pSelectedItem)
			{
				m_pSelectedItem->unselected();
			}
			m_pSelectedItem = currentItem;
			if (m_pSelectedItem)
			{
				m_pSelectedItem->selected();
			}
		}
	}
}